package org.marsson.bNav;

/**
 * Title:        BattleShip
 * @author       William Dubel
 * @version      1.0 07/28/2001
 */


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

import org.apache.log4j.Logger;
import org.apache.log4j.chainsaw.Main;

class Game extends Container
{
	protected PlayingField myField;
	private JPanel statusBar, messagePanel;
	private JLabel status1;
	private JLabel status2;
	private JLabel status3;
	//private JButton send;
	protected Point thePoint = new Point();
	protected Pedaco result;
	protected Connection myConnection;
	protected boolean demoRunning = true;
	protected int tirosDados=0;
	protected int acertos=0;
	protected int erros =0;
	public BattleShip battleship;
	public Logger logger;
	public Point lastShot=null;
	public char next = 'N';

	Game(String borderTitle, BattleShip battleship)
	{
		this.battleship = battleship;
		myField = new PlayingField(borderTitle);

		setLayout(new BorderLayout());
		add(myField, BorderLayout.CENTER);

		(messagePanel = new JPanel()).setLayout(new BoxLayout(messagePanel,BoxLayout.Y_AXIS));
		messagePanel.setBorder(new TitledBorder("Status da batalha"));
		status1 = new JLabel("Tiros dados:  "+tirosDados);
		status2 = new JLabel("Acertos:          "+acertos);
		status3 = new JLabel("Erros:              "+erros);
		
		
		//message = new JTextField();
		//message.addKeyListener(new KeyAdapter()
	  //{
	//	public void keyTyped (KeyEvent ke)
	//	{
		//	if (ke.getKeyChar()==ke.VK_ENTER)
		//	{
		//		if (myConnection!=null && myConnection.established())
		//		{
		//		   myConnection.sendObject(new ObjectPacket(message.getText()));
		//		}
		//		myField.addMessage("Dispatched: " + message.getText());
		//		message.setText("");
		//		if (BattleShip.soundOn()) Sound.sonar.play();
		//	}
		//}
	  //});

	//  ButtonHandler handle = new ButtonHandler();
		//send = new JButton("Send");
		//send.addActionListener(handle);

		//messagePanel.add(message, BorderLayout.CENTER);
		messagePanel.add(status1);
		messagePanel.add(status2);
		messagePanel.add(status3);
		add(messagePanel, BorderLayout.SOUTH);
	}

/*	private class ButtonHandler implements ActionListener
   {
	  public void actionPerformed( ActionEvent e )
	  {
			if (e.getSource() == send)
			{
				if (myConnection!=null && myConnection.established())
				{
					myConnection.sendObject(new ObjectPacket(message.getText()));
				}
				myField.addMessage("Dispatched: " + message.getText());
				message.setText("");
				if (BattleShip.soundOn()) Sound.sonar.play();
			}
		}
	}
	*/
	public void atira(){
		myField.addMessage("Sua Vez de Atirar...");
		Packet packet=null;
		thePoint = myField.getPoint();
		try {
			 packet = new Packet(thePoint.x,thePoint.y,next);
		} catch (InvalidPacketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		battleship.serialComm.writetoport(packet);
		//logger.debug("Enviando pacote");
		lastShot = thePoint;
		tirosDados++;
		status1.setText("Tiros dados:  "+tirosDados);
		
	}
	
	public void esperaTiro(){
		myField.addMessage("Aguardando o Tiro do Adversario...");
		//Espera receber um tiro do inimigo;
		try {
			while(battleship.serialComm.nextPacket==null)
			{Thread.sleep(500);
			
			}
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("Recebi um pacote");
		//pega o pacote.
		Packet packetanswer = battleship.serialComm.getNextPacket();
		//Verifica se acertou o ultimo tiro.
		if(lastShot!=null){
			System.out.println("Tem um tiro anterior");
			if(packetanswer.getAcertou()=='S'){
				myField.addMessage("Voce atingiu um navio no seu tiro anterior.");
				acertos++;
				status2.setText("Acertos:          "+acertos);
				status2.repaint();
				messagePanel.repaint();
				//Caso acertado, seta o ponto no mapa do inimigo.
				myField.setResult(lastShot, true);
			}
			else {
				//caso nao, seta a agua
				myField.addMessage("Voce nao acertou nada no seu tiro anterior");
				myField.setResult(lastShot, false);
				erros++;
				status3.setText("Erros:              "+erros);
				status3.repaint();
			}
			
		}
		Point p = new Point(packetanswer.getX(),packetanswer.getY());
		result = myField.getHit(p);
		//verifica o que ha no ponto onde o inimigo tentou.
		if (result.getNavio()!= Ships.SPLASH){
			next ='S';
		}
		else
			next='N';
		lastShot = null;
		
		
		
		
		//In real game this is sent to & recieved from opponent
		//thePoint.setLocation(packetanswer.getX(),packetanswer.getY());	
			//In real game this is sent to & recieved from opponent
		//myField.setResult(thePoint, result);
		
		try	{	Thread.sleep(10);	}
		
		catch	(InterruptedException ie)	{	ie.printStackTrace();	}
	
	}
}